package request

import (
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"lab.com/consts"
	"lab.com/define/errorcode"
)

// IurListReq 产学研列表查询参数
type IurListReq struct {
	Title     string `json:"title" form:"title" comment:"关键字"`
	IsAll     int    `json:"is_all" form:"is_all" validate:"oneof=0 10" comment:"是否是全部，0全部 10首页"`
	StartTime string `json:"start_time" form:"start_time" comment:"开始时间"`
	EndTime   string `json:"end_time" form:"end_time" comment:"结束时间"`
	PageOffset
}

// IurBasicInfo 产学研列表信息
type IurBasicInfo struct {
	Id            int    `json:"id"`
	Title         string `json:"title"`
	Abstract      string `json:"abstract"`
	Author        string `json:"author"`
	ImgPath       string `json:"img_path"`
	EstablishTime string `json:"establish_time"`
}

// IurListRes 产学研列表返回结果
type IurListRes struct {
	List  []IurBasicInfo `json:"list"`
	Count int            `json:"count"`
}

// CheckInputParams return input params
func (iur *IurListReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(iur); err != nil {
		return err
	}
	if errs, err := Validate(iur); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	iur.ParseOffset()
	return nil
}

// IurDetailReq 新闻公告详情查询参数
type IurDetailReq struct {
	ID int `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
}

// IurDetailRes 新闻公告详情返回结果
type IurDetailRes struct {
	IurInfo
	Content string `json:"content"`
}

// CheckInputParams return input params
func (iur *IurDetailReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(iur); err != nil {
		return err
	}
	if errs, err := Validate(iur); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	return nil
}

// IurAddReq 新闻公告新增参数
type IurAddReq struct {
	Title         string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	Abstract      string `json:"abstract" form:"abstract" validate:"required,max=100" comment:"摘要"`
	Author        string `json:"author" form:"author" validate:"required,max=20" comment:"作者"`
	Content       string `json:"content" form:"content" comment:"内容"`
	EstablishTime string `json:"establish_time" form:"establish_time" validate:"required" comment:"发布时间"`
	IsShow        int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	//IsHome        int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	ImgPath       string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
}

// CheckInputParams return input params
func (iur *IurAddReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(iur); err != nil {
		return err
	}
	if errs, err := Validate(iur); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// IurDeleteReq 新闻公告删除参数
type IurDeleteReq struct {
	ID int `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
}

// CheckInputParams return input params
func (iur *IurDeleteReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(iur); err != nil {
		return err
	}
	if errs, err := Validate(iur); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// IurEditReq 新闻公告编辑参数
type IurEditReq struct {
	ID            int    `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
	Title         string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	Abstract      string `json:"abstract" form:"abstract" validate:"required,max=100" comment:"摘要"`
	Author        string `json:"author" form:"author" validate:"required,max=20" comment:"作者"`
	Content       string `json:"content" form:"content" comment:"内容"`
	EstablishTime string `json:"establish_time" form:"establish_time" validate:"required" comment:"发布时间"`
	IsShow        int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	//IsHome        int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	ImgPath       string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
}

// CheckInputParams return input params
func (iur *IurEditReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(iur); err != nil {
		return err
	}
	if errs, err := Validate(iur); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// IurShowReq 新闻公告展示参数
type IurShowReq struct {
	ID     int `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
	IsShow int `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
}

// CheckInputParams return input params
func (iur *IurShowReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(iur); err != nil {
		return err
	}
	if errs, err := Validate(iur); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// IurHomeReq 新闻公告首页展示参数
type IurHomeReq struct {
	ID     int `json:"id" form:"id" validate:"required" comment:"新闻公告id"`
	IsHome int `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
}

// CheckInputParams return input params
func (iur *IurHomeReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(iur); err != nil {
		return err
	}
	if errs, err := Validate(iur); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// IurListInManageReq 新闻公告管理列表查询参数
type IurListInManageReq struct {
	Title string `json:"title" form:"title" comment:"关键字"`
	// IsShow    int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	// IsHome    int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	StartTime string `json:"start_time" form:"start_time" comment:"开始时间"`
	EndTime   string `json:"end_time" form:"end_time" comment:"结束时间"`
	PageOffset
}

// IurInfo 新闻公告列表信息
type IurInfo struct {
	Id            int    `json:"id"`
	Title         string `json:"title"`
	Abstract      string `json:"abstract"`
	Author        string `json:"author"`
	ImgPath       string `json:"img_path"`
	IsShow        int    `json:"is_show"`
	IsHome        int    `json:"is_home"`
	EstablishTime string `json:"establish_time"`
}

// IurListInManageRes 新闻公告列表返回结果
type IurListInManageRes struct {
	List  []IurInfo `json:"list"`
	Count int       `json:"count"`
}

// CheckInputParams return input params
func (iur *IurListInManageReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(iur); err != nil {
		return err
	}
	if errs, err := Validate(iur); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}

	iur.ParseOffset()
	return nil
}
